﻿using DryIoc;
using HandyControl.Controls;
using log4net;
using PlaceOrder_Core.Helper;
using PlaceOrder_Core.Services.Instances;
using PlaceOrder_Core.Services.Interfaces;
using PlaceOrder_Server.Extensions;
using PlaceOrder_Shared.Models.Common;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Interop;
using DialogResult = Prism.Services.Dialogs.DialogResult;


namespace PlaceOrder_Server.ViewModels
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        private readonly ILog _log;
        private readonly IUserHelper _userHelper;

        public static string StaticUserName="";
        public LoginViewModel( IEventAggregator aggregator,ILog log, IUserHelper userHelper)
        {
            _log = log;
            _userHelper = userHelper;
            ExecuteCommand = new DelegateCommand<string>(Execute);
            this.aggregator = aggregator;


            PassWord = "admin123";
            Execute("Login");
        }

        public string Title { get; set; } = "全益下单系统（服务端）";
        public string Icon { get; set; } = "../Images/lily.ico";


        private bool _isLoading;
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                if (_isLoading != value)
                {
                    _isLoading = value;
                    RaisePropertyChanged();
                }
            }
        }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            LoginOut();
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
        }

        #region Login

        private int selectIndex;

        public int SelectIndex
        {
            get { return selectIndex; }
            set { selectIndex = value; RaisePropertyChanged(); }
        }


        public DelegateCommand<string> ExecuteCommand { get; private set; }


        private string userName="admin";

        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }
        public string PassWord
        {
            get { return passWord; }
            set { passWord = value; RaisePropertyChanged(); }
        }
        private string passWord;

        private readonly IEventAggregator aggregator;

        

        private void Execute(string obj)
        {
            switch (obj)
            {
                case "Login": Login(); break;
                case "Cancel": LoginOut(); break;
                case "ResgiterPage": SelectIndex = 1; break;
                case "Return": SelectIndex = 0; break;
            }
        }


        async void Login()
        {
            IsLoading = true;

            AppLog.WriteInfo("登录成功哦");
            if (string.IsNullOrWhiteSpace(UserName) || string.IsNullOrWhiteSpace(PassWord))
            {
                Growl.Info("请输入用户名/密码", "LoginMsg");
                IsLoading = false; // 设置加载状态为 false
                return;
            }

            UserInfo userInfo = new UserInfo();
            userInfo.userName = UserName;
            userInfo.password = PassWord;

            var loginResult = await _userHelper.Login(userInfo,0);
            IsLoading = false; // 设置加载状态为 false

            if (loginResult.Item2)
            {
                if (UserHelper.UserName != "admin")
                {
                    Growl.Info("请使用admin登录账户", "LoginMsg");
                    return;
                }
            }
            else
            {
                Growl.Error("登录异常,请检查用户名及密码", "LoginMsg");
            }

            if (loginResult.Item1 != null && loginResult.Item2 == true)
            {
                string msg = "登录成功。" + "账户为：" + UserName;
                StaticUserName = UserName;
                aggregator.SendMessage(msg);
                AppLog.WriteInfo(msg);
                RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
            }
            else
            {
                string msg = "登录成功。" + "账户为：" + UserName;
                AppLog.WriteError(msg);
                aggregator.SendMessage(loginResult.Item1, "Login");
            }
        }



        void LoginOut()
        {
            RequestClose?.Invoke(new DialogResult(ButtonResult.No));

        }

        #endregion
    }
}
